import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-04
 * Time: 19:28
 */
@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder ret = new StringBuilder();
        ret.append(req.getProtocol());
        ret.append("<br>");
        ret.append(req.getMethod());
        ret.append("<br>");
        ret.append(req.getRequestURI());
        ret.append("<br>");
        ret.append(req.getQueryString());
        ret.append("<br>");
        ret.append(req.getContextPath());
        ret.append("<br>");

        ret.append("=====================================================<br>");

        Enumeration<String> headersNames = req.getHeaderNames();

        while(headersNames.hasMoreElements()) {
            String headerName = headersNames.nextElement();
            String headerValue = req.getHeader(headerName);
            ret.append(headerName + ":" + headerValue+"<br>");
        }

        //在响应中设置 body 类型方便浏览
        resp.setContentType("text/html; charset = utf8");
        resp.getWriter().write(ret.toString());
    }
}
